﻿using Common.Communal;
using ERP.Domain.CustomerSupplier;
using ERP.Domain.SalesManagement;
using IdGen;
using MediatR;
using Sell.Api.Read.Application.Command.SalesManagementCommand;
using Sell.DTO;
using SqlSugar;

namespace Sell.Api.Read.Application.CommandHandlers.SalesManagementCommandHandlers
{
    public class VerificationlistCommandHandlers : IRequestHandler<VerificationlistCommand, ApiResult<ApiPaging<verificationdto>>>
    {
        public ISqlSugarClient SqlSugar { get; }
        public VerificationlistCommandHandlers(ISqlSugarClient sqlSugar)
        {
            SqlSugar = sqlSugar;
        }
        /// <summary>
        /// 采购核价--列表
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ApiResult<ApiPaging<verificationdto>>> Handle(VerificationlistCommand request, CancellationToken cancellationToken)
        {
            ApiResult<ApiPaging<verificationdto>> res = new ApiResult<ApiPaging<verificationdto>>();
            res.Code = EnumCode.Ok;
            res.Message = "采购核价显示成功";
            /// 初始化查询：关联Verification表和Supplier表
            var query = SqlSugar.Queryable<Verification>()
                .LeftJoin<Supplier>((v, s) => v.SupplierId == s.Id)
                .Where(v => v.IsDelete == false).Select(
                (v, s) => new verificationdto
                {
                    Id = v.Id,
                    VerificationName = v.VerificationName,
                    VerificationCode = v.VerificationCode,
                    UpdateTime = v.UpdateTime,
                    SupplierId = v.SupplierId,
                    TaxRate = v.TaxRate,
                    Review = v.Review,
                    Effect = v.Effect,
                    Desc = v.Desc,
                    MaterialId = v.MaterialId,
                    IsDelete = v.IsDelete,
                    SupplierCode = s.SupplierCode,
                    SupplierName = s.SupplierName,
                    SupplierLevel = s.SupplierLevel,
                    People = s.People,
                    State = s.State,
                    Phone = s.Phone,
                });
            if (!string.IsNullOrEmpty(request.VerificationName))
            {
                query = query.Where(v => v.VerificationName.Contains(request.VerificationName));
            }
            if (request.SupplierId != null)
            {
                query = query.Where(v => v.SupplierId.Equals(request.SupplierId));
            }
            //SqlSugar分页查询实现
            ApiPaging<verificationdto> result = new ApiPaging<verificationdto>();
            result.TotalCount = query.Count();//查询总记录数
            result.TotalPage = (int)Math.Ceiling(result.TotalCount * 1.0 / request.pagesize);//计算总页数
            result.Data = query.Skip((request.pageindex - 1) * request.pagesize)//跳过前面的记录
                .Take(request.pagesize)//取指定页大小的记录
                .ToList();//执行查询并转为List
            res.Success(result, "查询成功");
            return res;
        }
    }
}